package touro.mancala.client;

import java.awt.Color;
import java.util.StringTokenizer;

public class Board {
	Store[] pitsAndStores;
	
	public Board (Player[] players, Color color) {
		this(players, 4, color);
	}
	/**
	 * Board constructor based on a string
	 * @param s string representation of a Board
	 */
	public Board(String s){
		pitsAndStores=new Store[14];
		Player[] players={new Player("Random1"), new Player("Random2")};
		//get array of # of stones in each of the pits(excluding stores)
		int[] stoneArray=processStoneString(s);
		
		for (int i = 0; i < 7; i++){

			if (i == 6) {
				pitsAndStores[i] = new Store(players[0]);
			} else {
				pitsAndStores[i] =new Pit(stoneArray[i],players[0], i, null);
			}
		}

		for (int i = 7; i < 14; i++){
			
			if (i == 6) {
				pitsAndStores[i] = new Store(players[0]);
			} else {
				pitsAndStores[i] =new Pit(stoneArray[i],players[0], i, null);
			}
		}
			
	}
	/**
	 * method to create an array holding the numbers of stones in each pit
	 * @param s string representation of a Board
	 * @return array of # of stones
	 */
	private int[] processStoneString(String s){
		
		StringTokenizer stok=new StringTokenizer(s,".");
		int[] answer=new int[14];
		int index=0;
		
		while(stok.hasMoreTokens()){
			if(index==6 || index==13){
				index++;
			}
			answer[index]=Integer.parseInt(stok.nextToken());
			index++;
		}
		return answer;
	}

	public Board(Player[] players, int numStones, Color color) {
		pitsAndStores = new Store[14];
		for (int i = 0; i < 7; i++){
			if (i == 6) {
				pitsAndStores[i] = new Store(players[0]);
			} else {
				pitsAndStores[i] = new Pit(numStones, players[0], i, color);
			}
		}

		for (int i = 7; i < 14; i++){
			if (i == 13) {
				pitsAndStores[i] = new Store(players[1]);
			} else {
				pitsAndStores[i] = new Pit(numStones, players[1], i, color);
			}
		}
	}
	
	public Store getStore (int index) {
		return pitsAndStores[index];
	}
	
	public Store getMyStore (Player p) {
		return (p.equals(pitsAndStores[6].getPlayer())) ? pitsAndStores[6] : pitsAndStores[13];
	}

	public Store[] getPitsAndStores() {
		return pitsAndStores;
	}
	
	public boolean isEmpty (Player p) {
		int ind = p.equals(pitsAndStores[6].getPlayer()) ? 0 : 7;
		for (int i = ind; i < 6+ind; i++) {
			if (pitsAndStores[i].getSize() > 0) {
				return false;
			}
		}
		return true;
	}
	
	public String toString(){
		StringBuffer sb=new StringBuffer();
		int s=0;
		for(int i=0;i<14;i++){
			if(i==6 ||i==13){
				
			}
			else{
				s=pitsAndStores[i].getSize();
				sb.append(s);
				sb.append(".");
				s=0;
			}
		}	
		return sb.toString();
	}
	/**
	 * overrided equals method
	 * this method does not consider the state of the stores
	 */
	public boolean equals(Object o){
		if(o instanceof Board){
			for(int i=0;i<14;i++){
				if(i==6 || i==13){
					
				}
				else{
					if(((Board)o).getPitsAndStores()[i].getSize()!=
						pitsAndStores[i].getSize()){
						return false;
					}
				}
			}
		}
		else{
			return false;
		}
		return true;
	}
	
	
	
}
